<?php


namespace App\Admin\Renderable;


use App\Admin\Repositories\NewHouse;
use App\Models\Building;
use App\Models\Cities;
use App\Models\FangAttrs;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class NewHouseTable extends LazyRenderable
{

    public function grid(): Grid
    {
        $id = $this->payload['id'] ?? null;
        // TODO: Implement grid() method.
        return Grid::make(new NewHouse(), function (Grid $grid)use ($id) {
            $grid->model()->where('building_id',$id);
            $grid->column('id')->sortable();
            $grid->column('building_id','楼盘名称')->display(function ($res){
                return Building::find($res)->title;
            });
            $grid->column('house_type_id','户型')->display(function ($res){
                return FangAttrs::find($res)->name;
            });
            $grid->column('forward','朝向')->display(function ($res){
                return FangAttrs::find($res)->name;
            });;
            $grid->column('fit_up','装修');
            $grid->column('area','建筑面积');
            $grid->column('avg_price','单价');
            $grid->column('price','总价')->display(function ($res){
                return $res."万元";
            });
            $grid->column('floor','楼层');
            $grid->column('category','物业类型');
//            $grid->column('images','组图');
//            $grid->column('desc');
//            $grid->column('latitude');
//            $grid->column('longitude');
            $grid->column('sale_status','售卖状态')->display(function ($res){
                if ($res == 1){
                    return '在售';
                }else{
                    return '售罄';
                }
            });
            $grid->column('province','省份')->display(function ($res){
                return Cities::find($res)->name;
            });
            $grid->column('city','城市')->display(function ($res){
                return Cities::find($res)->name;
            });;
            $grid->column('region','县区')->display(function ($res){
                return Cities::find($res)->name;
            });;
            $grid->showCreateButton()->setResource('/house/new');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }
}
